﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using admin.cyt369.com.Models;
using Admin.CYT369.Com.WebBase;

namespace admin.cyt369.com.Controllers
{
    public partial class PositionsController
    {
        #region GetPositionTree
        public IActionResult GetPositionTrees()
        {
            var organizations = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizations().ToArray();
            var departments = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartments().ToArray();
            var positions = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetPositions().ToArray();
            var list = _MakeOrganizationsTree(organizations, departments, positions, 0);

            return DefaultJson(list);
        }


        private List<admin.cyt369.com.Models.Orgs.OrgPositionTree> _MakeOrganizationsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Organization> organizations,
                                                                                                          IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                          IEnumerable<Admin.CYT369.Com.DAL.Domains.Position> positions,
                                                                                                          int parentOrgId)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgPositionTree>();

            foreach (var org in organizations.Where(c => c.ParentID == parentOrgId))
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgPositionTree(org);

                item.children = _MakePositionsTree(positions, 0, org.Id);
                item.children.AddRange(_MakeDepartmentsTree(departments, positions, 0, org.Id));
                item.children.AddRange(_MakeOrganizationsTree(organizations, departments, positions, org.Id));
                if (item.children.Count > 0)
                {
                    item.state = "open";
                    list.Add(item);
                }
            }

            return list;
        }

        private List<admin.cyt369.com.Models.Orgs.OrgPositionTree> _MakeDepartmentsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments, 
                                                                                                         IEnumerable<Admin.CYT369.Com.DAL.Domains.Position> positions,
                                                                                                         int parentDeptId, int orgId)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgPositionTree>();
            var depts = departments.Where(c => c.ParentID == parentDeptId && c.OrgID == orgId);

            foreach (var dept in depts)
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgPositionTree(dept);
                item.children = _MakePositionsTree(positions, dept.Id, orgId);
                item.children.AddRange(_MakeDepartmentsTree(departments, positions, dept.Id, orgId));
                if (item.children.Count > 0)
                {
                    item.state = "open";
                    list.Add(item);
                }
            }

            return list;
        }

        private List<admin.cyt369.com.Models.Orgs.OrgPositionTree> _MakePositionsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Position> positions, int deptId, int orgId)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgPositionTree>();

            foreach (var pos in positions.Where(c => c.OrgID == orgId && c.DepartmentID == deptId))
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgPositionTree(pos);

                list.Add(item);
            }

            return list;
        }
        #endregion

        #region GetOrgDepts
        [HttpGet]
        public IActionResult GetPositions()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);

            var organizationId = Request.Query.GetInteger("OrganizationID");
            var deptId = Request.Query.GetInteger("DepartmentID");
            var positionName = Request.Query.GetString("PositionName");
            int total = 0;

            var items = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetPositions(page, rows, ref total, organizationId, deptId, positionName);

            return DefaultJson(new { total = total, rows = items });
        }

        public IActionResult GetOrgDeptTrees()
        {
            var organizations = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetOrganizations().ToArray();
            var departments = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetDepartments().ToArray();
            var list = _MakeOrganizationsTree(organizations, departments, 0);

            return DefaultJson(list);
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree> _MakeOrganizationsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Organization> organizations,
                                                                                                          IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                          int parentOrgId)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree>();

            foreach (var org in organizations.Where(c => c.ParentID == parentOrgId))
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentTree(org);
                item.iconCls = "fa fa-support";
                list.Add(item);

                item.children = _MakeDepartmentsTree(departments, 0, org.Id);
                item.children.AddRange(_MakeOrganizationsTree(organizations, departments, org.Id));
                if (item.children.Count > 0)
                {
                    item.state = "open";
                }
            }

            return list;
        }

        private List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree> _MakeDepartmentsTree(IEnumerable<Admin.CYT369.Com.DAL.Domains.Department> departments,
                                                                                                         int parentDeptId, int orgId)
        {
            var list = new List<admin.cyt369.com.Models.Orgs.OrgDepartmentTree>();
            var depts = departments.Where(c => c.ParentID == parentDeptId && c.OrgID == orgId);

            foreach (var dept in depts)
            {
                var item = new admin.cyt369.com.Models.Orgs.OrgDepartmentTree(dept);
                list.Add(item);

                item.children = _MakeDepartmentsTree(departments, dept.Id, orgId);
                if (item.children.Count > 0)
                {
                    item.state = "open";
                }
            }

            return list;
        }
        #endregion

        /// <summary>
        /// 按部门ID获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPositionByDeptID(string id)
        {
            bool? enable = this.Request.Query.GetBoolean("Enable");
            var id_list = Admin.CYT369.Com.Utility.URLHelper.ArgumentToInts(id, new char[] { ',' });
            var item = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetPositionByDeptID(id_list, enable);

            return DefaultJson(item);
        }

        [HttpGet]
        public IActionResult GetPositionByID(int id)
        {
            var item = Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.GetPositionByID(id);

            return DefaultJson(item);
        }

        [HttpPost]
        public IActionResult SavePosition([FromForm]Admin.CYT369.Com.DAL.Domains.Position position)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (position != null && Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.SavePosition(position))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "保存失败";
            }

            return DefaultJson(resp);
        }

        [HttpDelete]
        public IActionResult DeletePosition(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (Admin.CYT369.Com.DAL.Services.OrganizationService.Instance.DeletePosition(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }
    }
}